package myJs.user.workflow.featureSelection.preprocess

import com.softwaremill.quicklens._
import myJs.Implicits._
import myJs.cps._
import AllCp.PreprocessT
import shared.pojo.WorkflowMission._
import myJs.myPkg.reactHookForm._
import myJs.tool.Tool
import myJs.tool.Tool._
import shared.Pojo._

import myJs.user.FeatureSelectionTool
import shared.pojo.WorkflowMission.ExtraData

import scala.collection.SeqMap

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.Utils._

/** Created by yz on 3/11/2020
  */
object InnerNormalCp extends PreprocessT with InnerNormalSoftT {

  case class Props(
      curI: Int,
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      datas: List[EachPreprocessData],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit],
      extraData: ExtraData
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    object FTool {

      def rowNameChange = (v: String) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).rowName).setTo(v)
        props.datasChange(newPreProcess)
      }

      def multipleChange = (v: String) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).multiple).setTo(v)
        props.datasChange(newPreProcess)
      }

      def removeModuleF =  {
        props.removeModule(props.curI)
      }

      def insertModule = (moduleName: String) => {
        props.insertModule(moduleName)
      }

    }

    useEffect(
     SyncIO{
        Tool.popover
      }.void,
      List()
    )

    useEffect(
      {
        val dbRowNames = props.extraData.rowNames
        val curRowName = props.datas(props.curI).rowName
        if (dbRowNames.nonEmpty && !dbRowNames.contains(curRowName)) {
          FTool.rowNameChange(dbRowNames.head)
        }else SyncIO.unit
      },
      List(props.extraData.rowNames)
    )

    val multipleMap =
      SeqMap("1" -> "1", "10000" -> messages("tenThousand"), "1000000" -> messages("aMillion"), "mean" -> "mean")
    val rowNameMap = props.extraData.rowNames.map { x =>
      (x, x)
    }.toSeqMap
    val multipleInfo = messages("multiplierInfo")
    val data = props.datas(props.curI)
    val rowName = data.rowName
    val multiple = data.multiple

    fieldset(
      paddingRight := "5px", border := "1px solid silver", margin := "0 2px", borderRadius := "4px",
      className := "northbtn myEachSoft",
      MyToolCp.Props(removeModule = FTool.removeModuleF, insertModule = FTool.insertModule).render,
      legend(s"${messages(nameStr)}"),
      div(
        RSelectCp.Props(
          value = rowName,
          label = messages("internalStandard"),
          name = "rowNames[]",
          optionMap = rowNameMap,
          onChange = FTool.rowNameChange
        ).render,
        SelectCp.Props(
          value = multiple,
          label = messages("multiplier"),
          name = "multiples[]",
          optionMap = multipleMap,
          onChange = FTool.multipleChange,
          questionInfo = multipleInfo
        ).render
      )
    )

  }

  def cp(
      curI: Int,
      datas: List[EachPreprocessData],
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit],
      extraData: ExtraData
  ) = {
    InnerNormalCp.Props(
      curI = curI,
      datas = datas,
      datasChange = datasChange,
      insertModule = insertModule,
      removeModule = removeModule,
      extraData = extraData
    ).render
  }

}
